package PAT;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class P {
    String name;
    Integer type;
    Integer value;

    public P(String name, Integer type, Integer value) {
        this.name = name;
        this.type = type;
        this.value = value;
    }


}

public class PAT1025 {
    public static void main(String[] args) {
        ArrayList<P> ps = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        while (n-- > 0) {
            int m = scanner.nextInt();
            while (m-- >0){
                ps.add(new P(scanner.next(),2-n,scanner.nextInt()));
            }
        }
        ps.sort(new Comparator<P>() {
            @Override
            public int compare(P o1, P o2) {
                if(o1.value.equals(o2.value)){
                    if(o1.type.equals(o2.type)){
                        return o2.name.compareTo(o1.name);
                    }else{
                        return o1.type - o2.type;
                    }
                }else{
                    return o2.value-o1.value;
                }
            }
        });

        System.out.println(ps.size());
        int index = 0;
        int num =0;
        int rate2= 0;
        int rate3= 0;
        int tempValue = 0;
        for (P p : ps) {
            int tp;
            if(tempValue == p.value){
                num++;
            }else{
                num = 0;
            }
            tempValue = p.value;
            if(p.type==1){
                rate3++;
                tp =rate3;
            }else{
                rate2++;
                tp = rate2;
            }
            System.out.printf("%s %d %d %d",p.name,index-num+1,p.type,tp);
            index ++;
            if(index <= ps.size()){
                System.out.println("");
            }
        }
    }
}
